package com.ysd.lis.controller.eq;


import cn.hutool.core.io.FileUtil;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.WkFile;
import com.ysd.lis.entity.eq.EqEquipmentImage;
import com.ysd.lis.request.eq.DelEquipmentImageDto;
import com.ysd.lis.service.WkFileService;
import com.ysd.lis.service.eq.EqEquipmentImageService;
import com.ysd.util.PropertySlash;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 设备管理-设备类型 前端控制器
 * </p>
 *
 * @author prs
 * @since 2024-07-31
 */
@RestController
@RequestMapping("/api/eq/eqEquipmentImage")
public class EqEquipmentImageController extends BaseController {

    @Value("${equipmentImagePath}")
    private String equipmentImagePath;
    private static final int MAX_POST_SIZE = 20 * 1024 * 1024;

    @Autowired
    private EqEquipmentImageService eqEquipmentImageService;
    @Autowired
    private WkFileService wkFileService;

    @ApiOperation("上传图片")
    @PostMapping("/uploadEqEquipmentImage")
    public Result uploadEqEquipmentImage(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        String equiptId = request.getParameter("equiptId");
        if (file == null) {
            return Result.fail("文件不能为空") ;
        }

            long size = file.getSize();
            if (size > MAX_POST_SIZE) {
                return Result.fail("文件最大为 10M！");
            }
            String folder = equipmentImagePath + PropertySlash.getSlash();
            if (!FileUtil.exist(folder)) {
                FileUtil.mkdir(folder);
            }
            String fileName = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS")) + file.getOriginalFilename();
            String path = folder + fileName;
            File files1 = new File(path);
            if (FileUtil.exist(files1)) {
                return Result.fail("文件已存在");
            }
            File file2 = FileUtil.writeBytes(file.getBytes(), path);
            if (file2.length() < 0) {
                return Result.fail("文件上传失败");
            }
            Map<String, String> map = new HashMap<>();
            map.put("name", fileName);
            map.put("path", "/image/equipment/" + fileName);

        return eqEquipmentImageService.uploadEqEquipmentImage(equiptId, map);

    }
    @ApiOperation("删除图片")
    @GetMapping("/deleteFileById")
    public Result deleteFileById(DelEquipmentImageDto delEquipmentImageDto) {
        EqEquipmentImage image = eqEquipmentImageService.getById(delEquipmentImageDto.getId());
        File file = new File(equipmentImagePath + PropertySlash.getSlash() + image.getImageName());
        if (file.isFile()) {
            file.delete();
        }
        eqEquipmentImageService.removeById(delEquipmentImageDto.getId());
        return Result.succ(1, "删除成功", null);
    }
    @ApiOperation("上传文档")
    @PostMapping("/uploadEqEquipmentDoc")
    public Result uploadEqEquipmentDoc(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) throws IOException {
        return  eqEquipmentImageService.uploadEqEquipmentDoc(file,request);
    }

    @ApiOperation("删除文档")
    @GetMapping("/deleteDocById")
    public Result deleteDocById(DelEquipmentImageDto delEquipmentImageDto) {
        WkFile wkFile = wkFileService.getById(delEquipmentImageDto.getId());
        File file = new File(wkFile.getDocPath());
        if (file.isFile()) {
            file.delete();
        }
        wkFileService.removeById(delEquipmentImageDto.getId());
        return Result.succ(1, "删除成功", null);
    }
}
